package com.industrial.premu.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuAddSubstractItem;
import com.industrial.premu.service.IPreMuAddSubstractItemService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 加减分项Controller
 *
 * @author SangGuangCan
 * @date 2024-01-23
 */
@RestController
@RequestMapping("/add_substract_item")
public class PreMuAddSubstractItemController extends BaseController
{
    @Autowired
    private IPreMuAddSubstractItemService preMuAddSubstractItemService;

    /**
     * 查询加减分项列表
     */
    @RequiresPermissions("industrial:add_substract_item:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuAddSubstractItem preMuAddSubstractItem)
    {
        startPage();
        List<PreMuAddSubstractItem> list = preMuAddSubstractItemService.selectPreMuAddSubstractItemList(preMuAddSubstractItem);
        return getDataTable(list);
    }

    /**
     * 加载加分项外层列表
     * @param params
     * @return
     */
    @GetMapping("/addSubstractItemList")
    public TableDataInfo addSubstractItemList(PreMuAddSubstractItem params){
        params.setItemType("1");
        params.setCounty(SecurityUtils.getLoginUser().getSysUser().getDistrict());
        List<PreMuAddSubstractItem> preMuAddSubstractItems = preMuAddSubstractItemService.addSubstractItemList(params);
        return getDataTable(preMuAddSubstractItems);
    }

    /**
     * <p>
     *     加载外层减分项列表
     * </p>
     * @param params
     * @return
     */
    @GetMapping("/substractItemList")
    public TableDataInfo substractItemInfo(PreMuAddSubstractItem params){
        //查询列表数据
        params.setItemType("2");
        params.setCounty(SecurityUtils.getLoginUser().getSysUser().getDistrict());
        List<PreMuAddSubstractItem> preMuAddSubstractItems = preMuAddSubstractItemService.addSubstractItemList(params);
        return getDataTable(preMuAddSubstractItems);
    }

    /**
     * 导出加减分项列表
     */
    @RequiresPermissions("industrial:add_substract_item:export")
    @Log(title = "加减分项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuAddSubstractItem preMuAddSubstractItem)
    {
        List<PreMuAddSubstractItem> list = preMuAddSubstractItemService.selectPreMuAddSubstractItemList(preMuAddSubstractItem);
        ExcelUtil<PreMuAddSubstractItem> util = new ExcelUtil<PreMuAddSubstractItem>(PreMuAddSubstractItem.class);
        util.exportExcel(response, list, "加减分项数据");
    }

    /**
     * 获取加减分项详细信息
     */
    @RequiresPermissions("industrial:add_substract_item:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return preMuAddSubstractItemService.selectPreMuAddSubstractItemById(id);
    }

    @GetMapping("/getNodeInfoById/{id}")
    public AjaxResult getNodeInfoById(@PathVariable("id") String id){
        return preMuAddSubstractItemService.getNodeInfoById(id);
    }


    @GetMapping("/loadSubNode/{parentId}")
    public AjaxResult loadSubNode(@PathVariable("parentId") String parentId){
        return preMuAddSubstractItemService.loadSubNode(parentId);
    }

    /**
     * 新增加减分项
     */
    @RequiresPermissions("industrial:add_substract_item:add")
    @Log(title = "加减分项", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuAddSubstractItem preMuAddSubstractItem)
    {
        return preMuAddSubstractItemService.insertPreMuAddSubstractItem(preMuAddSubstractItem);
    }

    /**
     * <p>
     *     删除加减分项
     * </p>
     * @param preMuAddSubstractItems
     * @return
     */
    @PostMapping( "/batchRemove")
    @RequiresPermissions("scoreitem:addSubstractItem:batchRemove")
    public AjaxResult batchRemove(@RequestBody List<PreMuAddSubstractItem> preMuAddSubstractItems){
        return preMuAddSubstractItemService.batchRemoveData(preMuAddSubstractItems);
    }

    /**
     * 获取加分项内层树表格
     * @param queryParams 查询参数  年、企业类型、加减分类型
     * @return
     */
    @GetMapping("/front")
    public Map<String,Object> frontPage(PreMuAddSubstractItem queryParams){
        HashMap<String, Object> paramMap = new HashMap<>();

//        queryParams.setCreateBy(SecurityUtils.getUserId());
        queryParams.setCounty(SecurityUtils.getLoginUser().getSysUser().getDistrict());
        List<PreMuAddSubstractItem> preMuAddSubstractItems = preMuAddSubstractItemService.treeList(queryParams);
        paramMap.put("data",preMuAddSubstractItems);
        paramMap.put("code",200);
        paramMap.put("count",preMuAddSubstractItems.size());
        return paramMap;
    }

    /**
     * 修改加减分项
     */
    @RequiresPermissions("industrial:add_substract_item:edit")
    @Log(title = "加减分项", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuAddSubstractItem preMuAddSubstractItem)
    {
        return toAjax(preMuAddSubstractItemService.updatePreMuAddSubstractItem(preMuAddSubstractItem));
    }

    /**
     * 删除加减分项
     */
    @RequiresPermissions("industrial:add_substract_item:remove")
    @Log(title = "加减分项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(preMuAddSubstractItemService.deletePreMuAddSubstractItemByIds(ids));
    }

    @RequiresPermissions("industrial:add_substract_item:removewc")
    @DeleteMapping("/removeDataByYearAndCompanyType")
    public  AjaxResult removeDataByYearAndCompanyType(@RequestBody PreMuAddSubstractItem preMuAddSubstractItem){
        preMuAddSubstractItemService.removeDataByYearAndCompanyType(preMuAddSubstractItem);
        return AjaxResult.success();
    }
}
